<?php defined('SYSPATH') or die('No direct access allowed.');
class lib_common{
	
	/* 多级select下拉菜单
	 * $array = array(
			0=>array(
				'key'=>3,
				'value'=>5,
				'sub'=>array(
					0=>array(
						'key'=>5,
						'value'=>9
					),
					1=>array(
						'key'=>9,
						'value'=>'dsufo'
					)
				)
			),
			1=>array(
				'key'=>'aaa',
				'value'=>'ddd'
			)
		);*/
	public static function select($name, array $options = NULL, $selected = NULL, $parent_disable = false, array $attributes = NULL){
		// Set the input name
		$attributes['name'] = $name;

		if (is_array($selected))
		{
			// This is a multi-select, god save us!
			$attributes['multiple'] = 'multiple';
		}
		
		if ( ! is_array($selected))
		{
			if ($selected === NULL)
			{
				// Use an empty array
				$selected = array();
			}
			else
			{
				// Convert the selected options to an array
				$selected = array( (string) $selected);
			}
		}
		
		foreach ($options as $key=>&$option){
			if (is_string($option)) $option = array('key'=>$key,'value'=>$option);
			if (is_array($option)) $option = self::option(0, $option,$selected,$parent_disable);
		}
		//var_dump($options);
		$options = "\n".implode("\n", $options);
		return '<select'.HTML::attributes($attributes).'>'.$options.'</select>';
	}
	private static function option($x,$option,$selected,$parent_disable){
		$have_sub = (bool)(isset($option['sub']) && !empty($option['sub']));
		$pre = (0 === $x)?'':'|__';
		
		$attributes['style'] = 'padding-left:'.(15*(int)$x).'px;';
		$attributes['value'] = $option['key'];
		if (in_array($option['key'], $selected)) $attributes['selected'] = 'selected';
		if ($have_sub && $parent_disable) $attributes['disabled'] = 'disabled';
		
		$options = array('<option'.HTML::attributes($attributes).'>'.$pre.$option['value'].'</option>');
		if ($have_sub){
			foreach ($option['sub'] as $sub){
				$options[] = self::option($x+1, $sub,$selected,$parent_disable);
			}
		}
		return "\n".implode("\n", $options);
	}
	
	public static function table(array $array,$tableAttributes = NULL,$trAttributes = NULL,$tdAttributes = NULL,$th = false,$thAttributes = NULL){
		$tableAttributes = array_filter((array)$tableAttributes);
		$trAttributes = array_filter((array)$trAttributes);
		$tdAttributes = array_filter((array)$tdAttributes);
		
		$t = '<table'.HTML::attributes($tableAttributes).'>';
		if ($th){
			if ($thAttributes === NULL)
				$thAttributes = $tdAttributes;
			else
				$thAttributes = array_filter((array)$thAttributes);
			$t .= '<thead><th'.HTML::attributes($thAttributes).'>'
			.implode('</th><th'.HTML::attributes($thAttributes).'>', array_shift($array))
			.'</th></thead>';
		}
		
		$t .= '<tbody>';
		foreach ($array as $array){
			$t .= '<tr'.HTML::attributes($trAttributes).'>';
			foreach ($array as $array){
				$t .= '<td'.HTML::attributes($tdAttributes).'>'.$array.'</td>';
			}
			$t .= '</tr>';
		}
		$t .= '</tbody>';
		$t .= '</table>';
		return $t;
	}
}